package com.wr.services.impl;

import com.wr.mappers.PersonalEntityMapper;
import com.wr.mappers.RoleUserEntityMapper;
import com.wr.mappers.UserMapper;
import com.wr.pojos.PersonalEntity;
import com.wr.pojos.RoleUserEntity;
import com.wr.pojos.UserEntity;
import com.wr.services.UserService;
import com.wr.utils.PageInfo;
import com.wr.utils.Utils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: UserServiceImpl
 * @Description: 用户接口实现类
 * @author 吴睿
 * @date 2016-11-29
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleUserEntityMapper roleUserMapper;

    @Resource
    private PersonalEntityMapper personalMapper;

    public UserEntity selectUser(String mobile, String password) {
        UserEntity userEntity = new UserEntity();
        userEntity.setMobile(mobile);
        userEntity.setPassword(password);
        List<UserEntity> userList = userMapper.selectUser(userEntity);
        if(userList.size()>0){
            userEntity = userList.get(0);
            return userEntity;
        }
        return null;
    }

    @Override
    public UserEntity selectUserByNameAndMobile(String username, String mobile) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(username);
        userEntity.setMobile(mobile);
        List<UserEntity> userList = userMapper.selectUserByNameAndMobile(userEntity);
        if(userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(UserEntity userEntity) {
        String userId = UUID.randomUUID().toString().replace("-","");
        userEntity.setId(userId);
        userEntity.setPassword(Utils.MD5(userEntity.getPassword()));
        userEntity.setCreateTime(Utils.formatLongDate(new Date()));
        userMapper.addUser(userEntity);

        RoleUserEntity roleUserEntity = new RoleUserEntity();
        roleUserEntity.setUserId(userId);
        roleUserEntity.setRoleId(2);
        roleUserMapper.insert(roleUserEntity);

        PersonalEntity personalEntity = new PersonalEntity();
        personalEntity.setUserId(userId);
        personalEntity.setAddTime(Utils.formatLongDate(new Date()));
        personalMapper.insertSelective(personalEntity);
    }

    @Override
    public UserEntity authentication(UserEntity user) {
        UserEntity userEntity = null;
        try {
            userEntity = userMapper.authentication(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userEntity;
    }

    @Override
    public UserEntity selectByUsername(String mobile) {
        UserEntity userEntity = new UserEntity();
        userEntity.setMobile(mobile);
        final List<UserEntity> list = userMapper.selectUserByNameAndMobile(userEntity);
        return list.get(0);
    }

    @Override
    public List<UserEntity> selectUserList(PageInfo pageInfo) {
        pageInfo.setCount(userMapper.countUserList());
        return userMapper.selectUserList(new RowBounds(pageInfo.getOffset(),pageInfo.getPageSize()));
    }

    @Override
    public void updateUserRole(RoleUserEntity roleUserEntity) {
        roleUserMapper.updateByPrimaryKey(roleUserEntity);
    }

    @Override
    public RoleUserEntity selectByUserAndRole(String userId) {
        List<RoleUserEntity> list = roleUserMapper.selectByUserId(userId);
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }
}
